﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing.Imaging;
using System.Drawing;

namespace els_web
{
    public partial class controls_button : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var text = this.Request.QueryString["t"];
            if (string.IsNullOrEmpty(text))
            {
                text = string.Empty;
            }

            var text_w = this.Request.QueryString["w"];
            if (string.IsNullOrEmpty(text))
            {
                text_w = string.Empty;
            }

            var text_n = this.Request.QueryString["n"];
            if (string.IsNullOrEmpty(text))
            {
                text_n = string.Empty;
            }

            var sMirror = this.Request.QueryString["m"];
            bool mirror = (!string.IsNullOrEmpty(sMirror));

            sMirror = this.Request.QueryString["k"];
            bool karantena = (!string.IsNullOrEmpty(sMirror));

            var rightImageWidth = 10; // in pixels
            var imageHeight = 42;
            var topPadding = 11; // top and bottom padding in pixels
            var sidePadding = 10; // side padding in pixels
            var textBrush = new SolidBrush(Color.White);
            var font = new Font("Arial Black", 15);
            var textBrush_warning = new SolidBrush(Color.OrangeRed);
            var font_warning = new Font("Arial Black", 15);
            var textBrush_info = new SolidBrush(Color.Black);
            var font_info = new Font("Arial Black", 15);

            //-------- Calculate the text's width
            // this graphics and bitmap object is just temporary. I could not
            // find a way to calculate the text' width witout having one.
            var bitmap = new Bitmap(300, 300);
            var graphics = Graphics.FromImage(bitmap);
            var textSize = graphics.MeasureString(text, font);
            bitmap.Dispose();
            graphics.Dispose();

            //-------- Create the graphics object
            var bitmapWidth = sidePadding * 2 + (int)textSize.Width;
            bitmap = new Bitmap(bitmapWidth, imageHeight);
            //bitmap = new Bitmap(500, 40);
            graphics = Graphics.FromImage(bitmap);

            // Draw the background
            System.Drawing.Image leftImage;
            System.Drawing.Image rightImage;
            if (!mirror)
            {
                leftImage = System.Drawing.Image.FromFile(this.Server.MapPath("~/Images/button-left.png"));
                rightImage = System.Drawing.Image.FromFile(this.Server.MapPath("~/Images/button-right.png"));
            }
            else
            {
                leftImage = System.Drawing.Image.FromFile(this.Server.MapPath("~/Images/button-left-x.png"));
                rightImage = System.Drawing.Image.FromFile(this.Server.MapPath("~/Images/button-right-x.png"));
            }
            if (karantena)
            {
                leftImage = System.Drawing.Image.FromFile(this.Server.MapPath("~/Images/button-left-k.png"));
                rightImage = System.Drawing.Image.FromFile(this.Server.MapPath("~/Images/button-right-k.png"));
            }

            graphics.DrawImage(leftImage, 0, 0, bitmapWidth - rightImageWidth, imageHeight);
            graphics.DrawImage(rightImage, bitmapWidth - rightImageWidth, 0, rightImageWidth, imageHeight);
            // These disposes are necessary, otherwise the files get locked
            leftImage.Dispose();
            rightImage.Dispose();


                bitmap.RotateFlip(RotateFlipType.RotateNoneFlipX);

            // Draw the text
            graphics.DrawString(text, font, textBrush, sidePadding, topPadding);
            graphics.DrawString(text_w, font_warning, textBrush_warning, 0, 0);
            graphics.DrawString(text_n, font_info, textBrush_info, 0, 20);
            graphics.ScaleTransform(0.7f, 0.7f);
            graphics.PageScale = 0.7f;

            //-------- Serve the Image

            this.Response.ContentType = "image/x-png";

            // The line below breaks on the production and development servers
            // (it does not break on my computer). I was getting the following
            // vague exception "A generic error occurred in GDI+". I found this
            // post that showed
            // me the work around http://aspalliance.com/319 . In short it says
            // that some image formats (like Png) require a seekable stream when
            // saving. The Response.OutputStream is not seekable so we
            // have to use a MemoryStream as an intermediary.
            //bitmap.Save(this.Response.OutputStream, ImageFormat.Png);

            var memStream = new System.IO.MemoryStream();
            bitmap.Save(memStream, ImageFormat.Png);
            memStream.WriteTo(this.Response.OutputStream);

            // Some cleanup, not sure if it is all needed
            this.Response.End();
            memStream.Dispose();
            graphics.Dispose();
            bitmap.Dispose();
        }
    }
}